package com.beici.waterghost.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.beici.waterghost.mapper.albumMapper;
import com.beici.waterghost.mapper.nodeMapper;
import com.beici.waterghost.model.album;
import com.beici.waterghost.model.idRandom;
import com.beici.waterghost.model.node;
import com.beici.waterghost.model.resultes;
import com.beici.waterghost.service.ablumService;
import com.beici.waterghost.utils.contextUtils;
import com.beici.waterghost.utils.takeUtils;
import com.tencentcloudapi.ame.v20190916.models.Album;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ablumServiceImpl implements ablumService {
    @Autowired
    private albumMapper albumserver;
    @Autowired
    private nodeMapper nodeServer;

    @Override
    public resultes quertyTagInfo() {
        List<album> albums = albumserver.selectList(new QueryWrapper<album>().select("ab_id", "ab_name"));
        return resultes.success("获取成功",albums);
    }

    @Override
    public resultes quertyLookSort() {
        List<album> albums = albumserver.selectList(new QueryWrapper<album>().orderByDesc("ab_look").select("ab_id", "ab_name"));
        List<album> orderAlbum = new ArrayList<>();
        for (int i = 0;i<albums.size() && i <5 ; i++) {

            orderAlbum.add(albums.get(i));
        }
        return resultes.success("获取成功",orderAlbum);
    }

    @Override
    public resultes quertyDetilsTag(Long abId) {
        // 通过abId查出标签所有信息
        album tag = (album) takeUtils.verfitySelect(albumserver.selectOne(new QueryWrapper<album>().eq("ab_id", abId)));
        tag.setAbLook(tag.getAbLook()+1);
        albumserver.updateById(tag);
        Map info = new LinkedHashMap();
        // 查出所有帖子是否有该id
        List<node> nodes = nodeServer.selectList(new QueryWrapper<node>().select("node_id","node_look","node_tag","node_images"));
        List list = new ArrayList();
        for (node node : nodes) {
            // 拿到帖子的标签
            String nodeTag = node.getNodeTag();
            if (nodeTag.contains(String.valueOf(abId))){
                // 如果有则拿到第一张图片
                String img = node.getNodeImages().split(",")[0];
                Map map = new LinkedHashMap();
                map.put("nodeId",node.getNodeId());
                map.put("nodeImages",img);
                map.put("nodeLook",node.getNodeLook());
                list.add(map);
            }
        }
        info.put("tag",tag);
        info.put("node",list);
        return resultes.success("获取成功",info);
    }

    @Override
    public resultes addAlbum(Integer userId,String abIp,String abName,String abMess) {

        // 查看当前标签是否已经存在
        Integer abCount = albumserver.selectCount(new QueryWrapper<album>().eq("ab_name", abName));
        if (abCount>0){
            album abId = albumserver.selectOne(new QueryWrapper<album>().eq("ab_name", abName).select("ab_id"));
            return resultes.divResultes(abId.getAbId(),"当前标签已存在自动选择",204);
        }
        // 判断内容是否有敏感词汇
        boolean b = contextUtils.veifityContext(abName);
        if (!b){
            return resultes.error("您创建的专辑存在敏感词汇");
        }
        album a = new album();
        a.setUserId(userId);
        a.setAbName(abName);
        a.setAbIp(abIp);
        a.setUserDate(new Date());
        a.setAbMessage(abMess);
        //生成id
        long longRandom = idRandom.getLongRandom();
        a.setAbId(longRandom);
        int insert = albumserver.insert(a);
        if (insert<0){
            return resultes.error("添加失败");
        }
        return resultes.success("添加成功已自动选择",longRandom);
    }

    @Override
    public resultes getAlbumAll() {
        List<album> abAll = albumserver.selectList(new QueryWrapper<album>().orderByDesc("ab_look").select("ab_id","ab_name","ab_look","ab_message"));
        return resultes.success(null,abAll);
    }
}
